﻿using FBroSharp;
using FBroSharp.Callback;
using FBroSharp.Const;
using FBroSharp.DataType;
using FBroSharp.Event;
using FBroSharp.Lib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using 无窗口纯数据创建URL;

namespace 无窗口纯数据创建URL
{
    public class Event : FBroInitEvent
    {
        //即将处理命令行
        public override void OnBeforeCommandLineProcessing(string process_type, IFBroSharpCommandLine cmd)
        {

            ////关闭GPU加速
            cmd.DisableGpuBlockList();
            cmd.DisableGpuCache();

            Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + process_type + cmd.IsReadOnly() + cmd.GetProgram());
        }

    //初始化完毕
    public override void OnContextInitialized()
    {
        Console.WriteLine(MethodBase.GetCurrentMethod().Name);


        FBroSharpRequest sharp_request = new FBroSharpRequest();
        sharp_request.SetURL("https://api.fbrowser.site:8443/");
        sharp_request.SetMethod("GET");

        //添加协议头，这里用list添加，只添加了一个UA
        List<FBroSharpStringMap> heard_list = new List<FBroSharpStringMap>();

        heard_list.Add(new FBroSharpStringMap
        {
            key = "User-Agent",
            value = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.97 Safari/537.36 Core/1.116.438.400 QQBrowser/13.0.6071.400"
        });
        sharp_request.SetHeaderMap_Array(heard_list);

        // FBroSharpRequestContext sharp_request_context;
        URLEvent sharp_event = new URLEvent();
        FBroSharpURLRequest.Create(sharp_request, default, sharp_event, 123456);
 
    }

        public override void OnBeforeChildProcessLaunch(IFBroSharpCommandLine command_line)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + command_line.IsReadOnly() + command_line.GetProgram());
        }

    }


public class URLEvent : FBroSharpURLEvent
{
    //用于存放全部数据
    private MemoryStream responseStream_;

    private BinaryWriter writer_;

    public override void Start(long flag, IFBroSharpURLRequest request)
    {
        responseStream_ = new MemoryStream();
        writer_ = new BinaryWriter(responseStream_);

        Console.WriteLine("Name:{0} Flag:{1} Request url:{2} Referrer url:{3}", MethodBase.GetCurrentMethod().Name, flag, request.GetRequestStatus(), request.GetRequest().GetURL(), request.GetRequest().GetReferrerURL());
    }

    public override void End(long flag)
    {
        Console.WriteLine("Name:{0} Flag:{1}", MethodBase.GetCurrentMethod().Name, flag);
           
        writer_ = null;
        responseStream_ = null;
           

        Program.is_close_ = true;
    }

    public override bool GetAuthCredentials(long flag, bool isProxy, string host, int port, string realm, string scheme, IFBroSharpAuthCallback callback)
    {
        Console.WriteLine("Name:{0} Flag:{1}", MethodBase.GetCurrentMethod().Name, flag);
        return false;
    }

    public override void OnDownloadData(long flag, IFBroSharpURLRequest request, Stream data)
    {
        Console.WriteLine("Name:{0} Flag:{1} RequestStatus:{2}", MethodBase.GetCurrentMethod().Name, flag, request.GetRequestStatus());

        //如果存在多数据这个会多次调用本事件，因此数据先进行暂存到writer_中
        var readBytes = new byte[data.Length];
        data.Read(readBytes, 0, readBytes.Length);
        writer_.Write(readBytes);
    }

    public override void OnDownloadProgress(long flag, IFBroSharpURLRequest request, long current, long total)
    {
        Console.WriteLine("Name:{0} Flag:{1} RequestStatus:{2}", MethodBase.GetCurrentMethod().Name, flag, request.GetRequestStatus());
    }

    public override void OnRequestComplete(long flag, IFBroSharpURLRequest request)
    {
        Console.WriteLine("Name:{0} Flag:{1} RequestStatus:{2}", MethodBase.GetCurrentMethod().Name, flag, request.GetRequestStatus());

        //执行完毕再显示数据
        Console.WriteLine(Encoding.UTF8.GetString(responseStream_.ToArray()));

    }

    public override void OnUploadProgress(long flag, IFBroSharpURLRequest request, long current, long total)
    {
        Console.WriteLine("Name:{0} Flag:{1} RequestStatus:{2}", MethodBase.GetCurrentMethod().Name, flag, request.GetRequestStatus());
    }


}
}